testbatting2007 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2007testbattingrating.csv")
testbatting2008 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2008testbattingrating.csv")
testbatting2009 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2009testbattingrating.csv")
testbatting2010 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2010testbattingrating.csv")
testbatting2011 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2011testbattingrating.csv")
testbatting2012 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2012testbattingrating.csv")
testbatting2013 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2013testbattingrating.csv")
testbatting2014 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2014testbattingrating.csv")
testbatting2015 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2015testbattingrating.csv")
testbatting2016 <- read.csv("D:\\Vishal\\III year\\Data Analytics\\Assignment II\\Player Ratings\\2016testbattingrating.csv")

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dataTestBatting <- bind_rows(testbatting2007, testbatting2008, testbatting2009, testbatting2010,
                            testbatting2011, testbatting2012, testbatting2013, testbatting2014,
                            testbatting2015, testbatting2016)
## Warning in bind_rows_(x, .id): Unequal factor levels: coercing to character
## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector

## Warning in bind_rows_(x, .id): binding character and factor vector,
## coercing into character vector
summary(dataTestBatting)
##      Name               Rating        LogRating    
##  Length:1000        Min.   :171.0   Min.   :2.233  
##  Class :character   1st Qu.:330.0   1st Qu.:2.519  
##  Mode  :character   Median :454.0   Median :2.657  
##                     Mean   :485.4   Mean   :2.653  
##                     3rd Qu.:632.2   3rd Qu.:2.801  
##                     Max.   :936.0   Max.   :2.971  
##                                     NA's   :1
library(VIM)
## Loading required package: colorspace
## Loading required package: grid
## Loading required package: data.table
## 
## Attaching package: 'data.table'
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## VIM is ready to use. 
##  Since version 4.0.0 the GUI is in its own package VIMGUI.
## 
##           Please use the package to use the new (and old) GUI.
## Suggestions and bug-reports can be submitted at: https://github.com/alexkowa/VIM/issues
## 
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
## 
##     sleep
aggr(dataTestBatting)

dataTestBatting <- dataTestBatting %>%
  group_by(Name) %>%
  summarise(avg = mean(Rating))

set.seed(20)

batcluster <- kmeans(dataTestBatting[, 2], 5)

batcluster$cluster <- as.factor(batcluster$cluster)


library(DT)
library(ggplot2)

ggplot(dataTestBatting, aes(dataTestBatting$Name, avg, color = batcluster$cluster)) +
  geom_point(size = 2) +
  scale_color_manual(labels = c("Good", "Best", "Useless", "Better", "Average"), values = c("blue", "red", "green", "magenta", "black")) +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
  ggtitle("Test Batting Ratings(2007-2016)")

dat <- arrange(dataTestBatting, desc(avg)) %>%
  mutate(rank = 1:nrow(dataTestBatting))

dataTestBatting <- merge(dataTestBatting, dat, by  = "Name") 
dataTestBatting
##                        Name    avg.x    avg.y rank
## 1               Aftab Ahmed 315.0000 315.0000  189
## 2           Mohammad Hafeez 344.0000 344.0000  170
## 3             Raqibul Hasan 235.0000 235.0000  242
## 4           A B de Villiers 773.9000 773.9000    8
## 5                  A Barath 356.5000 356.5000  161
## 6             A C Gilchrist 662.0000 662.0000   31
## 7                 A C Voges 626.0000 626.0000   41
## 8                 A D Hales 405.0000 405.0000  139
## 9               A D Mathews 611.7143 611.7143   45
## 10               A Flintoff 505.0000 505.0000   77
## 11               A G Prince 676.8000 676.8000   26
## 12               A J Doolan 335.0000 335.0000  178
## 13              A J Redmond 255.6667 255.6667  229
## 14              A J Strauss 654.8333 654.8333   33
## 15                 A Kumble 277.5000 277.5000  215
## 16                   A Lyth 285.0000 285.0000  208
## 17               A M Rahane 644.0000 644.0000   36
## 18                 A Mishra 248.0000 248.0000  233
## 19                 A N Cook 711.1000 711.1000   17
## 20             A N Petersen 465.8000 465.8000   97
## 21                A Symonds 586.5000 586.5000   53
## 22             Abdur Razzak 171.0000 171.0000  265
## 23             Abdur Rehman 224.0000 224.0000  252
## 24              Adnan Akmal 339.5000 339.5000  175
## 25              Aftab Ahmed 314.5000 314.5000  191
## 26            Ahmed Shehzad 488.0000 488.0000   88
## 27              Asad Shafiq 588.6667 588.6667   51
## 28                Azhar Ali 671.3333 671.3333   29
## 29               B A Stokes 497.6667 497.6667   83
## 30  B B B B B S M Warnapura 453.0000 453.0000  106
## 31    B B B B S M Warnapura 349.0000 349.0000  164
## 32             B B McCullum 603.7778 603.7778   48
## 33               B J Haddin 496.1250 496.1250   85
## 34                B J Hodge 454.0000 454.0000  105
## 35              B J Watling 430.1429 430.1429  122
## 36             B K G Mendis 466.0000 466.0000   96
## 37                  B Kumar 275.5000 275.5000  216
## 38                    B Lee 309.5000 309.5000  192
## 39                 B P Nash 463.3333 463.3333  100
## 40             B R M Taylor 386.0000 386.0000  149
## 41               C A Pujara 631.2000 631.2000   38
## 42              C D Cumming 406.0000 406.0000  136
## 43            C F K van Wyk 331.0000 331.0000  179
## 44                C H Gayle 628.0000 628.0000   39
## 45               C H Morris 254.0000 254.0000  231
## 46             C J Anderson 382.6667 382.6667  152
## 47             C J L Rogers 669.5000 669.5000   30
## 48           C K Kapugedera 307.6667 307.6667  194
## 49           C R Brathwaite 347.0000 347.0000  166
## 50               C R Ervine 227.0000 227.0000  249
## 51               C R Woakes 231.0000 231.0000  244
## 52               D A Warner 703.6000 703.6000   20
## 53                  D Elgar 427.2500 427.2500  123
## 54             D G Brownlie 449.0000 449.0000  108
## 55                  D Ganga 406.0000 406.0000  137
## 56              D J G Sammy 302.1667 302.1667  198
## 57              D J J Bravo 548.0000 548.0000   64
## 58              D L Vettori 549.5000 549.5000   62
## 59                D M Bravo 593.0000 593.0000   50
## 60        D P M Jayawardene 745.5000 745.5000   11
## 61                D R Flynn 323.6000 323.6000  184
## 62                 D Ramdin 405.3000 405.3000  138
## 63                D S Smith 315.3000 315.3000  188
## 64                D W Steyn 229.6667 229.6667  247
## 65             E Chigumbura 231.0000 231.0000  245
## 66             E J G Morgan 303.0000 303.0000  197
## 67              E J M Cowan 475.5000 475.5000   91
## 68        F D M Karunaratne 440.0000 440.0000  114
## 69             F du Plessis 626.7500 626.7500   40
## 70             Faisal Iqbal 340.6667 340.6667  172
## 71                G C Smith 732.1429 732.1429   16
## 72                G Gambhir 583.5000 583.5000   54
## 73                G P Swann 352.0000 352.0000  162
## 74             G S Ballance 479.0000 479.0000   90
## 75      H A P W Jayawardena 401.7000 401.7000  142
## 76         H D R Thirimanne 340.2500 340.2500  174
## 77           H D Rutherford 338.2500 338.2500  177
## 78                H H Gibbs 581.0000 581.0000   55
## 79                 H M Amla 740.7000 740.7000   13
## 80             H M R Herath 214.0000 214.0000  258
## 81              H Masakadza 392.0000 392.0000  148
## 82           Habibul Bashar 519.0000 519.0000   71
## 83          Harbhajan Singh 277.7778 277.7778  214
## 84              I J L Trott 688.6000 688.6000   24
## 85               I K Pathan 395.0000 395.0000  145
## 86                 I R Bell 638.1000 638.1000   37
## 87             Imran Farhat 498.8333 498.8333   80
## 88              Imrul Kayes 318.1429 318.1429  186
## 89           Inzamam-ul-Haq 735.0000 735.0000   15
## 90                J A Burns 421.5000 421.5000  126
## 91              J A Rudolph 363.8333 363.8333  158
## 92              J Blackwood 468.3333 468.3333   95
## 93              J C Buttler 420.0000 420.0000  127
## 94               J D P Oram 537.0000 537.0000   67
## 95                J D Ryder 539.0000 539.0000   66
## 96            J D S Neesham 437.6667 437.6667  116
## 97           J E C Franklin 279.5000 279.5000  212
## 98                 J E Root 693.7500 693.7500   22
## 99               J E Taylor 237.0000 237.0000  239
## 100              J H Kallis 814.4286 814.4286    5
## 101               J K Silva 554.3333 554.3333   59
## 102           J L Pattinson 274.5000 274.5000  217
## 103            J M Bairstow 464.0000 464.0000   99
## 104                 J M How 443.0000 443.0000  110
## 105              J O Holder 384.3333 384.3333  151
## 106              J P Duminy 433.2500 433.2500  118
## 107              Javed Omar 372.0000 372.0000  157
## 108         Junaid Siddique 408.2000 408.2000  135
## 109             K A Edwards 416.3333 416.3333  131
## 110          K C Brathwaite 403.2000 403.2000  140
## 111          K C Sangakkara 861.1111 861.1111    1
## 112         K D K Vithanage 254.6667 254.6667  230
## 113   K K K K K K D Karthik 359.3333 359.3333  160
## 114               K L Rahul 265.0000 265.0000  223
## 115            K O A Powell 351.5000 351.5000  163
## 116           K P Pietersen 741.1429 741.1429   12
## 117          K S Williamson 646.6667 646.6667   35
## 118            K T G Prasad 200.0000 200.0000  261
## 119            Kamran Akmal 565.0000 565.0000   57
## 120            Khaled Masud 289.0000 289.0000  206
## 121         Khurram Manzoor 307.5000 307.5000  195
## 122           L D Chandimal 530.2000 530.2000   69
## 123           L M P Simmons 237.0000 237.0000  240
## 124             L P C Silva 472.0000 472.0000   94
## 125             L R Johnson 379.5000 379.5000  154
## 126              L R Taylor 702.1111 702.1111   21
## 127                L Ronchi 417.5000 417.5000  129
## 128               L Vincent 515.0000 515.0000   73
## 129              Litton Das 267.5000 267.5000  222
## 130               M A Starc 339.5000 339.5000  176
## 131           M A Vermeulen 292.0000 292.0000  205
## 132               M D Craig 425.0000 425.0000  124
## 133          M D K J Perera 346.0000 346.0000  168
## 134            M D K Perera 278.0000 278.0000  213
## 135            M E K Hussey 759.0000 759.0000    9
## 136            M F Maharoof 243.0000 243.0000  236
## 137             M G Johnson 330.5000 330.5000  180
## 138             M G Vandort 402.0000 402.0000  141
## 139              M J Clarke 758.8889 758.8889   10
## 140             M J Guptill 418.2500 418.2500  128
## 141               M J North 472.5000 472.5000   93
## 142               M J Prior 555.1250 555.1250   58
## 143             M J Santner 297.0000 297.0000  202
## 144               M J Vijay 462.3750 462.3750  101
## 145              M L Hayden 831.0000 831.0000    4
## 146                M Morkel 245.0000 245.0000  235
## 147             M N Samuels 475.3000 475.3000   92
## 148              M N Waller 279.6667 279.6667  211
## 149             M P Vaughan 554.0000 554.0000   60
## 150               M R Marsh 272.0000 272.0000  218
## 151            M S Atapattu 503.0000 503.0000   78
## 152               M S Dhoni 566.2500 566.2500   56
## 153            M S Sinclair 385.3333 385.3333  150
## 154                M S Wade 349.0000 349.0000  165
## 155             M V Boucher 508.2000 508.2000   76
## 156             Mahmudullah 417.4286 417.4286  130
## 157        Mashrafe Mortaza 251.0000 251.0000  232
## 158           Misbah ul Haq 679.1111 679.1111   25
## 159               Moeen Ali 395.6667 395.6667  144
## 160       Mohammad Ashraful 392.6667 392.6667  147
## 161         Mohammad Hafeez 452.7778 452.7778  107
## 162          Mohammad Rafiq 318.0000 318.0000  187
## 163         Mohammad Yousuf 854.5000 854.5000    2
## 164           Mominul Haque 549.0000 549.0000   63
## 165         Mushfiqur Rahim 513.5000 513.5000   74
## 166            N D McKenzie 454.5000 454.5000  104
## 167             N Deonarine 363.7500 363.7500  159
## 168             N M Hauritz 302.0000 302.0000  199
## 169                N M Lyon 193.0000 193.0000  262
## 170           N R D Compton 329.2500 329.2500  183
## 171        N T Paranavitana 465.6667 465.6667   98
## 172                N V Ojha 221.0000 221.0000  255
## 173             Naeem Islam 240.0000 240.0000  238
## 174           Nasir Hossain 441.4000 441.4000  112
## 175              Nazimuddin 269.0000 269.0000  221
## 176                O A Shah 228.0000 228.0000  248
## 177              P A Jaques 588.0000 588.0000   52
## 178               P A Patel 281.5000 281.5000  209
## 179         P D Collingwood 662.0000 662.0000   32
## 180              P G Fulton 260.0000 260.0000  227
## 181              P J Hughes 501.8000 501.8000   79
## 182              P M Nevill 347.0000 347.0000  167
## 183              P M Siddle 263.5000 263.5000  225
## 184                P Utseya 206.0000 206.0000  260
## 185               Q de Kock 434.5000 434.5000  117
## 186              R A Jadeja 299.0000 299.0000  200
## 187                R Ashwin 442.2000 442.2000  111
## 188                R Dravid 710.4000 710.4000   18
## 189              R G Sharma 498.0000 498.0000   81
## 190              R J Harris 270.0000 270.0000  220
## 191            R J Peterson 258.0000 258.0000  228
## 192          R J Sidebottom 183.0000 183.0000  264
## 193               R McLaren 247.0000 247.0000  234
## 194               R N Lewis 222.0000 222.0000  254
## 195               R O Hinds 218.0000 218.0000  257
## 196              R R Sarwan 623.8000 623.8000   42
## 197              R S Bopara 341.0000 341.0000  171
## 198              R S Morton 297.0000 297.0000  203
## 199             R T Ponting 779.6667 779.6667    7
## 200            R W Chakabva 242.0000 242.0000  237
## 201             Rajin Saleh 441.0000 441.0000  113
## 202           Raqibul Hasan 235.0000 235.0000  243
## 203              S B Styris 509.0000 509.0000   75
## 204                S C Cook 439.0000 439.0000  115
## 205             S C Ganguly 607.5000 607.5000   46
## 206             S C J Broad 375.1111 375.1111  155
## 207           S Chanderpaul 831.2500 831.2500    3
## 208           S Chattergoon 191.0000 191.0000  263
## 209                S D Hope 236.0000 236.0000  241
## 210              S D Robson 305.0000 305.0000  196
## 211                S Dhawan 495.5000 495.5000   86
## 212               S E Marsh 394.2500 394.2500  146
## 213                S J Benn 230.0000 230.0000  246
## 214               S K Raina 308.1667 308.1667  193
## 215              S M Katich 595.2500 595.2500   49
## 216             S M Pollock 488.0000 488.0000   89
## 217             S O Dowrich 263.5000 263.5000  226
## 218             S P D Smith 619.5000 619.5000   44
## 219             S P Fleming 674.0000 674.0000   27
## 220           S R Tendulkar 736.7143 736.7143   14
## 221              S R Watson 532.1429 532.1429   68
## 222          S T Jayasuriya 498.0000 498.0000   82
## 223             S T R Binny 224.0000 224.0000  253
## 224               S van Zyl 330.5000 330.5000  181
## 225             Salman Butt 461.2500 461.2500  102
## 226           Sarfraz Ahmed 539.6667 539.6667   65
## 227           Shahid Afridi 457.5000 457.5000  103
## 228         Shahriar Nafees 430.8333 430.8333  121
## 229         Shakib Al Hasan 529.7778 529.7778   70
## 230          Shamsur Rahman 280.0000 280.0000  210
## 231             Shan Masood 271.0000 271.0000  219
## 232            Shoaib Malik 415.3333 415.3333  132
## 233           Shuvagata Hom 214.0000 214.0000  259
## 234           Sikandar Raza 381.0000 381.0000  153
## 235       T A M Siriwardana 431.0000 431.0000  119
## 236                T Bavuma 431.0000 431.0000  120
## 237            T G McIntosh 319.0000 319.0000  185
## 238             T G Southee 264.1429 264.1429  224
## 239             T M Dilshan 619.6667 619.6667   43
## 240            T M K Mawoyo 315.0000 315.0000  190
## 241            T Panyangara 226.0000 226.0000  250
## 242             T R Ambrose 374.0000 374.0000  156
## 243             T T Bresnan 293.0000 293.0000  204
## 244         T T Samaraweera 646.8333 646.8333   34
## 245                 T Taibu 516.0000 516.0000   72
## 246            T W M Latham 497.0000 497.0000   84
## 247             Tamim Iqbal 552.6667 552.6667   61
## 248            Tanvir Ahmed 225.0000 225.0000  251
## 249            Taufeeq Umar 446.3750 446.3750  109
## 250             U T Khawaja 340.3333 340.3333  173
## 251              Umar Akmal 605.5000 605.5000   47
## 252           V D Philander 401.5000 401.5000  143
## 253                 V Kohli 674.0000 674.0000   28
## 254                V Sehwag 709.1667 709.1667   19
## 255               V Sibanda 330.0000 330.0000  182
## 256            V V S Laxman 690.2000 690.2000   23
## 257                W P Saha 288.0000 288.0000  207
## 258              W P U Vaas 409.0000 409.0000  134
## 259            W U Tharanga 344.7778 344.7778  169
## 260            Wasim Jaffer 495.0000 495.0000   87
## 261            X M Marshall 299.0000 299.0000  201
## 262            Yasir Hameed 412.7500 412.7500  133
## 263             Younis Khan 788.2000 788.2000    6
## 264            Yuvraj Singh 423.1667 423.1667  125
## 265             Zaheer Khan 221.0000 221.0000  256
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p <- plot_ly(dataTestBatting, x = ~Name, y = ~avg.x, type = 'scatter', 
             mode = 'markers', color = batcluster$cluster, 
             text = ~paste('Rank: ', rank))

p